package me.weijing.common.http;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

import me.weijing.common.AppManager;
import android.content.Context;

public abstract class AsyncPlainHttpRequestBase extends AsyncHttpRequestBase {

    public AsyncPlainHttpRequestBase(Context context) {
        super(context);
        mCallback = new HttpProgressCallbackImpl();
    }

    public abstract void processResult(byte[] response);

    @Override
    public void cancel() {
        ((HttpProgressCallbackImpl) mCallback).setCanceled(true);
        super.cancel();
    }

    public void aGet(RequestParams reqParam) {

        try {
            String realurl = buildUrl(reqParam);
            mThreadPool.execute(new HttpRequestGet(getUrl(), realurl, mCallback, gExtraHeader));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                processFailed(EFailType.EFAILTYPE_SYS, FAILTYPE_CLIENT_CREATETHREAD, null);
                processFinish();
            } catch (Exception e2) {
                e2.printStackTrace();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public void aPost(RequestParams reqParam) {

        try {
            String realurl = buildUrl(null);
            mThreadPool.execute(new HttpRequestPost(getUrl(), realurl, mCallback, gExtraHeader, reqParam));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                processFailed(EFailType.EFAILTYPE_SYS, FAILTYPE_CLIENT_CREATETHREAD, null);
                processFinish();
            } catch (Exception e2) {
                e2.printStackTrace();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public void aPostFile(String filePath, String fileName, Map<String, String> extraParam) {

        try {
            String realurl = buildUrl(null);
            String key = "uploadfile";
            RequestParams reqParam = new RequestParams(extraParam);
            reqParam.put(key, new FileInputStream(new File(filePath)), fileName);
            mThreadPool.execute(new HttpRequestPost(getUrl(), realurl, mCallback, gExtraHeader, reqParam));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                processFailed(EFailType.EFAILTYPE_SYS, FAILTYPE_CLIENT_CREATETHREAD, null);
                processFinish();
            } catch (Exception e2) {
                e2.printStackTrace();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    @Override
    public void launchProgress() {
        

    }

    @Override
    public void processCanceled() {
        

    }

    @Override
    public void publishProgress(int progress) {
        

    }

    @Override
    public void processFinish() {
        

    }

    class HttpProgressCallbackImpl implements HttpProgressCallback {
        private boolean bCanceled = false;

        public void setCanceled(boolean bCancel) {
            this.bCanceled = bCancel;
        }

        @Override
        public void startProgress() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (checkSessionTag() == false)
                            return;
                        launchProgress();

                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void progressPublish(final int progress) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (checkSessionTag() == false)
                            return;
                        publishProgress(progress);

                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            });
        }

        /**
         * 返回的请求数
         * 
         * @param data
         *            为null表示请求失败
         */
        @Override
        public void interpret(final byte[] data) {
            if (null == data) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (checkSessionTag() == false)
                                return;
                            processFailed(EFailType.EFAILTYPE_SYS, FAILTYPE_NETFAIL, null);

                        } catch (Exception e) {
                            e.printStackTrace();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                        try {
                            processFinish();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                });
            } else {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (checkSessionTag() == false)
                            return;
                        try {
                            if (AppManager.isActivityDestroyed(mContext)) {
                                // processFailed(EFailType.EFAILTYPE_APP,0,
                                // "",jsonObj);
                                // fix when net
                            } else {
                                processResult(data);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                        try {
                            processFinish();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        }
                    }
                });
            }
        }

        @Override
        public void serverFail(final int httpCode) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (checkSessionTag() == false)
                        return;
                    try {
                        processFailed(EFailType.EFAILTYPE_APP, httpCode, null);

                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    try {
                        processFinish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            });

        }

        @Override
        public boolean isCanceled() {
            return bCanceled;
        }

        @Override
        public void interpret(byte[] data, Map<String, List<String>> responseHeaders) {
            interpret(data);
        }
    }

}
